package com.example;

import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class CourseDialogController {
    @FXML private Label titleLabel;
    @FXML private TextField nameField;
    @FXML private TextField creditsField;
    @FXML private ComboBox<String> departmentComboBox;
    @FXML private ComboBox<String> courseTypeComboBox;
    @FXML private ComboBox<String> semesterComboBox;
    @FXML private TextArea descriptionArea;

    private Stage dialogStage;
    private Course course;
    private boolean confirmed = false;
    private CourseDAO courseDAO;

    @FXML
    public void initialize() {
        courseDAO = new CourseDAO();

        // 初始化院系列表
        ObservableList<String> departments = FXCollections.observableArrayList(
            "计算机科学与技术学院",
            "电子信息工程学院",
            "机械工程学院",
            "经济管理学院",
            "外国语学院"
        );
        departmentComboBox.setItems(departments);

        // 初始化课程类型列表
        ObservableList<String> courseTypes = FXCollections.observableArrayList(
            "必修",
            "选修"
        );
        courseTypeComboBox.setItems(courseTypes);

        // 初始化学期列表
        ObservableList<String> semesters = FXCollections.observableArrayList(
            "2023-2024-1",
            "2023-2024-2",
            "2024-2025-1",
            "2024-2025-2"
        );
        semesterComboBox.setItems(semesters);
    }

    public void setDialogStage(Stage dialogStage) {
        this.dialogStage = dialogStage;
    }

    public void setCourse(Course course) {
        this.course = course;

        if (course != null) {
            // 编辑模式
            titleLabel.setText("编辑课程");
            nameField.setText(course.getName());
            creditsField.setText(String.valueOf(course.getCredits()));
            departmentComboBox.setValue(course.getDepartment());
            courseTypeComboBox.setValue(course.getCourseType());
            semesterComboBox.setValue(course.getSemester());
            descriptionArea.setText(course.getDescription());
        } else {
            // 添加模式
            titleLabel.setText("添加课程");
            nameField.clear();
            creditsField.clear();
            departmentComboBox.getSelectionModel().selectFirst();
            courseTypeComboBox.getSelectionModel().selectFirst();
            semesterComboBox.getSelectionModel().selectFirst();
            descriptionArea.clear();
        }
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    @FXML
    private void handleSave() {
        if (isInputValid()) {
            String name = nameField.getText().trim();
            double credits = Double.parseDouble(creditsField.getText().trim());
            String department = departmentComboBox.getValue();
            String courseType = courseTypeComboBox.getValue();
            String semester = semesterComboBox.getValue();
            String description = descriptionArea.getText().trim();

            if (course == null) {
                // 添加新课程
                course = new Course(0, name, credits, department, description, courseType, semester);
                if (courseDAO.addCourse(course)) {
                    confirmed = true;
                    dialogStage.close();
                } else {
                    showAlert(Alert.AlertType.ERROR, "错误", "添加课程失败");
                }
            } else {
                // 更新现有课程
                course.setName(name);
                course.setCredits(credits);
                course.setDepartment(department);
                course.setCourseType(courseType);
                course.setSemester(semester);
                course.setDescription(description);

                if (courseDAO.updateCourse(course)) {
                    confirmed = true;
                    dialogStage.close();
                } else {
                    showAlert(Alert.AlertType.ERROR, "错误", "更新课程失败");
                }
            }
        }
    }

    @FXML
    private void handleCancel() {
        dialogStage.close();
    }

    private boolean isInputValid() {
        String errorMessage = "";

        if (nameField.getText() == null || nameField.getText().trim().isEmpty()) {
            errorMessage += "课程名称不能为空\n";
        }

        if (creditsField.getText() == null || creditsField.getText().trim().isEmpty()) {
            errorMessage += "学分不能为空\n";
        } else {
            try {
                double credits = Double.parseDouble(creditsField.getText().trim());
                if (credits <= 0) {
                    errorMessage += "学分必须大于0\n";
                }
            } catch (NumberFormatException e) {
                errorMessage += "学分必须是有效的数字\n";
            }
        }

        if (departmentComboBox.getValue() == null) {
            errorMessage += "请选择所属院系\n";
        }

        if (courseTypeComboBox.getValue() == null) {
            errorMessage += "请选择课程类型\n";
        }

        if (semesterComboBox.getValue() == null) {
            errorMessage += "请选择开课学期\n";
        }

        if (errorMessage.isEmpty()) {
            return true;
        } else {
            showAlert(Alert.AlertType.ERROR, "输入错误", errorMessage);
            return false;
        }
    }

    private void showAlert(Alert.AlertType type, String title, String content) {
        Alert alert = new Alert(type);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(content);
        alert.showAndWait();
    }
} 